package com.qf.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.PageCommonBeanUtils;
import com.qf.staff.common.qo.SupplierQo;
import com.qf.staff.common.vo.SupplierVo;
import com.qf.staff.mapper.SupplierMapper;
import com.qf.staff.pojo.Supplier;
import com.qf.staff.service.SupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Resource
    SupplierMapper supplierMapper;

    @Override
    public IPage<SupplierVo> list(int page, int size, SupplierQo supplierQo, int number) {
        try {
            QueryWrapper<Supplier> qw = new QueryWrapper<>();
            if (supplierQo.getSupplierName() != null) {
                qw.eq("supplier_name", supplierQo.getSupplierName());
            }
            if (supplierQo.getPhone() != null) {
                qw.eq("phone", supplierQo.getPhone());
            }
            if (number == 1) {
                qw.orderByAsc("supplier_id");
            }
            if (number == 2) {
                qw.orderByDesc("entry_number");
            }
            if (number == 3) {
                qw.orderByDesc("money");
            }
            return PageCommonBeanUtils.copyPage(supplierMapper.selectPage(new Page<>(page, size), qw),
                    new Page<>(), SupplierVo::new);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public int delete(SupplierQo supplierQo) {
        try {
            Supplier supplier = new Supplier();
            BeanUtils.copyProperties(supplierQo, supplier);
            return supplierMapper.updateByStatus(supplier);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Override
    public int modify(SupplierQo supplierQo) {
        try {
            Supplier supplier = new Supplier();
            BeanUtils.copyProperties(supplierQo, supplier);
            return supplierMapper.updateById(supplier);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public int add(SupplierQo supplierQo) {
        try {
            Supplier supplier = new Supplier();
            BeanUtils.copyProperties(supplierQo, supplier);
            return supplierMapper.insert(supplier);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Transactional
    @Override
    public int deleteAll(List<Integer> ids, int status) {
        try {
            int num = supplierMapper.updateByAllStatus(ids, status);
            if (num < ids.size()) {
                throw new ServiceException(ResultCode.DELETE_ERROR);
            }
            return num;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }
}
